package UCM;

import Source.EditAirport;
import Views.ViewEditAirport;

/**
 *
 * @author Joshua King
 */
public class UCEditAirport implements UCController {
    private ViewEditAirport view;
    private EditAirport model = new EditAirport();
    

    public UCEditAirport(ViewEditAirport view) {
        this.view = view;
        view.loadTo(model);
    }
    
    
    /*
Precondition: Logged on as manager and already clicked on edit airport

   1. Manager changes details and clicks the change button.
   2. System loads data to the model.
   3. System checks validity of data.
   4. System commits data.
   5. System refreshes the edit airport UI indicating success. 

Postcondition: A flights information is updated and the manager is on the view airports page.

Unsuccessful Outcome:

    * 3a) The details entered by the manager are inappropriate.
         1. System refreshes the UI and explains errors to the manager. 
    * 4a) System can't commit data.
         1. System updates the UI informing the manager that the service is currently unavailable. 
    * 4b) System receives notice that an error has occurred.
         1. System updates the UI informing the manager that there was an internal error. 
     */
    public void run() {
        //2. System loads data to the model.
        view.loadData();
        //3. System checks validity of data.
        if (model.checkValidity()) {
            //4. System commits data.
            if (model.commitData()) {
                //5. System refreshes the edit airport UI indicating success.
                view.refresh();
            } else {
//                4a) System can't commit data.
//                1. System updates the UI informing the manager that the service is currently unavailable. 
//                4b) System receives notice that an error has occurred.
//                1. System updates the UI informing the manager that there was an internal error.
                view.updateFailure();
            }
        } else {
//          3a) The details entered by the manager are inappropriate.
//          1. System refreshes the UI and explains errors to the manager. 
            view.updateFailure();
        }      
    }
}
